<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>部门管理</title>
    <!-- Bootstrap 核心 CSS 文件 -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
      <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
    <!-- Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>

    <!-- 删除按钮操作插件 -->
    <link rel="stylesheet" href="/static/js/plugins/sweetalert2/sweetalert2.min.css">
    <script src="/static/js/plugins/sweetalert2/sweetalert2.min.js"></script>

</head>
<body class="hold-transition skin-black sidebar-mini">
    <div th:replace="common/fragment :: link"></div>
    <div class="wrapper">
        <div th:replace="common/fragment :: navbar"></div>
        <div th:replace="common/fragment :: menu"></div>
        <div class="content-wrapper">
            <section class="content-header">
                <h1>部门管理</h1>
            </section>
            <section class="content">
                <div class="box">
                    <!--高级查询--->
                    <form class="form-inline" id="searchForm" action="/department/list" method="post">
                        <input type="hidden" name="currentPage" id="currentPage" value="1">
                        <a href="#" class="btn btn-success btn-input" style="margin: 10px">
                            <span class="glyphicon glyphicon-plus"></span> 添加
                        </a>
                    </form>
                    <!--编写内容-->
                    <div class="box-body table-responsive ">
                        <table class="table table-hover table-bordered table-striped">
                            <thead>
                            <tr>
                                <th>编号</th>
                                <th>部门名称</th>
                                <th>部门编号</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                                <tr th:each="department:${pageInfo.list}">
                                    <td th:text="${department.id}">1</td>
                                    <td th:text="${department.name}">开发部</td>
                                    <td th:text="${department.sn}">DEV</td>
                                    <td>               <!--  th:data-json="${department.toJson}" -->
                                        <a href="#" class="btn btn-info btn-xs btn-input" th:data-json="${department.toJson()}">
                                            <span class="glyphicon glyphicon-pencil"></span> 编辑
                                        </a>
                                        <!-- ======== 删除："|/department/delete?id=${department.id}|"=========  -->
                                        <a class="btn btn-danger btn-xs btn-delete" th:data-url="|/department/delete?id=${department.id}|" >
                                            <span class="glyphicon glyphicon-trash"></span> 删除
                                        </a>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        <div th:replace="common/fragment :: page"></div>
                    </div>
                </div>
            </section>
        </div>
        <div th:replace="common/fragment :: footer"></div>
    </div>
    <div class="modal fade" tabindex="-1" role="dialog" id="myModal">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">部门编辑</h4>
                </div>
                <form action="/department/saveOrUpdate" method="post">
                    <input type="hidden" name="id" id="id">
                    <div class="modal-body">
                        <div class="form-group">
                            <label for="name">名称</label>
                            <input type="text" class="form-control" name="name" id="name" placeholder="名称">
                        </div>
                        <div class="form-group">
                            <label for="sn">缩写</label>
                            <input type="text" class="form-control" name="sn" id="sn" placeholder="缩写">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">保存</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
    <script>
        // 部门添加/编辑; 弹出模态框， 编辑数据回显
        $(".btn-input").click(function () {
            console.log(11);

            var department = $(this).data('json');
            console.log(department);
            $("#myModal input").val(''); // 设置为空
            if (department){ // 编辑时，数据回显
                $("#name").val(department.name);
                $("#sn").val(department.sn);
                $("#id").val(department.id);
            }

            $("#myModal").modal('show'); // 弹出模态框

        });

        // $(".btn-delete").click(function () {
        //     var url = $(this).data("url");
        //     console.log(url);
        //     Swal.fire({
        //         title: '确定要删除',
        //         text: "没有办法撤销",
        //         icon: 'warning',
        //         showCancelButton: true,
        //         confirmButtonColor: '#3085d6',
        //         cancelButtonColor: '#d33',
        //         confirmButtonText: '是的',
        //         cancelButtonText: '取消'
        //     }).then((result) => {
        //         if(result.value) {
        //             // 点了确定做什么，由开发者决定
        //             location.href=url;
        //         }
        //     });
        // })

    </script>
</html>